﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace Bus.Dal
{
	/// <summary>
	/// 数据访问类:tb_custom
	/// </summary>
	public partial class tb_custom
	{
		public tb_custom()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("cid", "tb_custom"); 
		}

        public int Lost(int cid,bool status)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_Custom set islost=@stus");
            strSql.Append(" where cid=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,10),
                    new SqlParameter ("@stus",SqlDbType .Bit)
			};
            parameters[0].Value = cid;
            parameters[1].Value = status;
            return DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        }
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string name)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from tb_custom");
            strSql.Append(" where idnum=@cid");
			SqlParameter[] parameters = {
					new SqlParameter("@cid", SqlDbType.VarChar ,18)
			};
			parameters[0].Value = name;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}

        public bool LoginCus(string name, string pwd)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tb_Custom");
            strSql.Append(" where [islost]=0 and [cname]=@nm and [password]=@pwd");
            SqlParameter[] parameters = {
					new SqlParameter("@nm", SqlDbType.VarChar,10),
                    new SqlParameter ("@pwd",SqlDbType .VarChar ,10)
			};
            parameters[0].Value = name;
            parameters[1].Value = pwd;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Bus.Model.CustomModel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into tb_custom(");
			strSql.Append("logid,tb__id,cname,password,id,sex,age,idnum,islost,ctype,regdate,coastid,cardnumber,routeid,nm)");
			strSql.Append(" values (");
			strSql.Append("@logid,@tb__id,@cname,@password,@id,@sex,@age,@idnum,@islost,@ctype,@regdate,@coastid,@cardnumber,@routeid,@nm)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@logid", SqlDbType.Int,4),
					new SqlParameter("@tb__id", SqlDbType.Int,4),
					new SqlParameter("@cname", SqlDbType.VarChar,10),
					new SqlParameter("@password", SqlDbType.VarChar,10),
					new SqlParameter("@id", SqlDbType.VarChar,10),
					new SqlParameter("@sex", SqlDbType.Int,4),
					new SqlParameter("@age", SqlDbType.Int,4),
					new SqlParameter("@idnum", SqlDbType.VarChar,18),
					new SqlParameter("@islost", SqlDbType.Bit,1),
					new SqlParameter("@ctype", SqlDbType.Int,4),
					new SqlParameter("@regdate", SqlDbType.DateTime),
					new SqlParameter("@coastid", SqlDbType.Int,4),
					new SqlParameter("@cardnumber", SqlDbType.VarChar,18),
					new SqlParameter("@routeid", SqlDbType.Int,4),
                                        new SqlParameter("@nm", SqlDbType.VarChar,100)};
			parameters[0].Value = model.logid;
			parameters[1].Value = model.tb__id;
			parameters[2].Value = model.cname;
			parameters[3].Value = model.password;
			parameters[4].Value = model.id;
			parameters[5].Value = model.sex;
			parameters[6].Value = model.age;
			parameters[7].Value = model.idnum;
			parameters[8].Value = model.islost;
			parameters[9].Value = model.ctype;
			parameters[10].Value = model.regdate;
			parameters[11].Value = model.coastid;
			parameters[12].Value = model.cardnumber;
			parameters[13].Value = model.routeid;
            parameters[14].Value = model.Nmcrad;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Bus.Model.CustomModel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update tb_custom set ");
			strSql.Append("logid=@logid,");
			strSql.Append("tb__id=@tb__id,");
			strSql.Append("cname=@cname,");
			strSql.Append("password=@password,");
			strSql.Append("id=@id,");
			strSql.Append("sex=@sex,");
			strSql.Append("age=@age,");
			strSql.Append("idnum=@idnum,");
			strSql.Append("islost=@islost,");
			strSql.Append("ctype=@ctype,");
			strSql.Append("regdate=@regdate,");
			strSql.Append("coastid=@coastid,");
			strSql.Append("cardnumber=@cardnumber,");
			strSql.Append("routeid=@routeid");
			strSql.Append(" where cid=@cid");
			SqlParameter[] parameters = {
					new SqlParameter("@logid", SqlDbType.Int,4),
					new SqlParameter("@tb__id", SqlDbType.Int,4),
					new SqlParameter("@cname", SqlDbType.VarChar,10),
					new SqlParameter("@password", SqlDbType.VarChar,10),
					new SqlParameter("@id", SqlDbType.VarChar,10),
					new SqlParameter("@sex", SqlDbType.Int,4),
					new SqlParameter("@age", SqlDbType.Int,4),
					new SqlParameter("@idnum", SqlDbType.VarChar,18),
					new SqlParameter("@islost", SqlDbType.Bit,1),
					new SqlParameter("@ctype", SqlDbType.Int,4),
					new SqlParameter("@regdate", SqlDbType.DateTime),
					new SqlParameter("@coastid", SqlDbType.Int,4),
					new SqlParameter("@cardnumber", SqlDbType.VarChar,18),
					new SqlParameter("@routeid", SqlDbType.Int,4),
					new SqlParameter("@cid", SqlDbType.Int,4)};
			parameters[0].Value = model.logid;
			parameters[1].Value = model.tb__id;
			parameters[2].Value = model.cname;
			parameters[3].Value = model.password;
			parameters[4].Value = model.id;
			parameters[5].Value = model.sex;
			parameters[6].Value = model.age;
			parameters[7].Value = model.idnum;
			parameters[8].Value = model.islost;
			parameters[9].Value = model.ctype;
			parameters[10].Value = model.regdate;
			parameters[11].Value = model.coastid;
			parameters[12].Value = model.cardnumber;
			parameters[13].Value = model.routeid;
			parameters[14].Value = model.cid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int cid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_custom ");
			strSql.Append(" where cid=@cid");
			SqlParameter[] parameters = {
					new SqlParameter("@cid", SqlDbType.Int,4)
			};
			parameters[0].Value = cid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string cidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_custom ");
			strSql.Append(" where cid in ("+cidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Bus.Model.CustomModel GetModel(int cid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 cid,logid,tb__id,cname,password,id,sex,age,idnum,islost,ctype,regdate,coastid,cardnumber,routeid,nm from tb_custom ");
			strSql.Append(" where cid=@cid");
			SqlParameter[] parameters = {
					new SqlParameter("@cid", SqlDbType.Int,4)
			};
			parameters[0].Value = cid;

			Bus.Model.CustomModel model=new Bus.Model.CustomModel();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["cid"]!=null && ds.Tables[0].Rows[0]["cid"].ToString()!="")
				{
					model.cid=int.Parse(ds.Tables[0].Rows[0]["cid"].ToString());
				}
				if(ds.Tables[0].Rows[0]["logid"]!=null && ds.Tables[0].Rows[0]["logid"].ToString()!="")
				{
					model.logid=int.Parse(ds.Tables[0].Rows[0]["logid"].ToString());
				}
				if(ds.Tables[0].Rows[0]["tb__id"]!=null && ds.Tables[0].Rows[0]["tb__id"].ToString()!="")
				{
					model.tb__id=int.Parse(ds.Tables[0].Rows[0]["tb__id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["cname"]!=null && ds.Tables[0].Rows[0]["cname"].ToString()!="")
				{
					model.cname=ds.Tables[0].Rows[0]["cname"].ToString();
				}
				if(ds.Tables[0].Rows[0]["password"]!=null && ds.Tables[0].Rows[0]["password"].ToString()!="")
				{
					model.password=ds.Tables[0].Rows[0]["password"].ToString();
				}
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=ds.Tables[0].Rows[0]["id"].ToString();
				}
				if(ds.Tables[0].Rows[0]["sex"]!=null && ds.Tables[0].Rows[0]["sex"].ToString()!="")
				{
					model.sex=int.Parse(ds.Tables[0].Rows[0]["sex"].ToString());
				}
				if(ds.Tables[0].Rows[0]["age"]!=null && ds.Tables[0].Rows[0]["age"].ToString()!="")
				{
					model.age=int.Parse(ds.Tables[0].Rows[0]["age"].ToString());
				}
				if(ds.Tables[0].Rows[0]["idnum"]!=null && ds.Tables[0].Rows[0]["idnum"].ToString()!="")
				{
					model.idnum=ds.Tables[0].Rows[0]["idnum"].ToString();
				}
				if(ds.Tables[0].Rows[0]["islost"]!=null && ds.Tables[0].Rows[0]["islost"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["islost"].ToString()=="1")||(ds.Tables[0].Rows[0]["islost"].ToString().ToLower()=="true"))
					{
						model.islost=true;
					}
					else
					{
						model.islost=false;
					}
				}
				if(ds.Tables[0].Rows[0]["ctype"]!=null && ds.Tables[0].Rows[0]["ctype"].ToString()!="")
				{
					model.ctype=int.Parse(ds.Tables[0].Rows[0]["ctype"].ToString());
				}
				if(ds.Tables[0].Rows[0]["regdate"]!=null && ds.Tables[0].Rows[0]["regdate"].ToString()!="")
				{
					model.regdate=DateTime.Parse(ds.Tables[0].Rows[0]["regdate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["coastid"]!=null && ds.Tables[0].Rows[0]["coastid"].ToString()!="")
				{
					model.coastid=int.Parse(ds.Tables[0].Rows[0]["coastid"].ToString());
				}
				if(ds.Tables[0].Rows[0]["cardnumber"]!=null && ds.Tables[0].Rows[0]["cardnumber"].ToString()!="")
				{
					model.cardnumber=ds.Tables[0].Rows[0]["cardnumber"].ToString();
				}
				if(ds.Tables[0].Rows[0]["routeid"]!=null && ds.Tables[0].Rows[0]["routeid"].ToString()!="")
				{
					model.routeid=int.Parse(ds.Tables[0].Rows[0]["routeid"].ToString());
				}
                if (ds.Tables[0].Rows[0]["nm"] != null && ds.Tables[0].Rows[0]["nm"].ToString() != "")
                {
                    model.Nmcrad = ds.Tables[0].Rows[0]["nm"].ToString();
                }
                else
                {
                    model.Nmcrad = string.Empty;
                }
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select cid,logid,tb__id,cname,password,id,sex,age,idnum,islost,ctype,regdate,coastid,cardnumber,routeid ");
			strSql.Append(" FROM tb_custom ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" cid,logid,tb__id,cname,password,id,sex,age,idnum,islost,ctype,regdate,coastid,cardnumber,routeid ");
			strSql.Append(" FROM tb_custom ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM tb_custom ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.cid desc");
			}
			strSql.Append(")AS Row, T.*  from tb_custom T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "tb_custom";
			parameters[1].Value = "cid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

